package com.alex.statistics.controller;

import com.alex.statistics.method.spatialAnalysis.*;
import com.alex.statistics.pojo.Result;
import com.alex.statistics.pojo.request.spatialAnalysis.*;
import com.alex.statistics.pojo.result.spatialAnalysis.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Tag(name = "空间分析", description = "包含核密度估计、全局/局部空间自相关分析")
@RequestMapping("/spatial")
public class SpatialAnalysisController {

    /* ------------ 空间分布分析 ------------ */
    @Autowired
    private KernelDensityService kdeService;

    /* ------------ 空间自相关分析 ------------ */
    @Autowired
    private GlobalMoranIService globalMoranIService;

    @Autowired
    private LocalLisaService localLisaService;


    // ================== 核密度估计 ==================

    @Operation(
            summary = "核密度估计计算",
            description = """
                    ### 算法说明
                    使用高斯核函数计算空间点密度分布：
                    - 带宽参数建议使用Silverman法则自动计算
                    - 输出结果可直接用于热力图渲染"""
    )
    @PostMapping("/kde")

    public Result<KernelDensityResult> calculateKDE(@RequestBody KernelDensityRequest request) {
        return Result.ok( kdeService.calculate(request));
    }

    @Operation(
            summary = "全局Moran's I空间自相关分析",
            description = """
                    ### 功能说明
                    计算全局空间自相关指数，评估空间分布模式：
                    - Moran's I ∈ [-1,1]  
                    - >0表示聚集，<0表示离散
                    
                    ### 使用建议
                    1. 权重矩阵推荐使用Queen邻接或距离衰减函数生成
                    2. 首次调用建议设置permutations≥999"""
    )
    @PostMapping("/global-moran")

    public Result<GlobalMoranIResult> globalMoranI(@RequestBody @Parameter(description = "全局空间自相关请求参数") GlobalMoranIRequest request) {
        GlobalMoranIResult result = globalMoranIService.calculate(request);
        return Result.ok(result);
    }

    // ================== 局部LISA ==================
    @PostMapping("/local-lisa")
    @Operation(summary = "局部LISA（空间自相关分析）")
    public Result<LocalLisaResult> localLisa(@RequestBody @Parameter(description = "局部空间自相关请求参数") LocalLisaRequest request) {
        LocalLisaResult result = localLisaService.calculate(request);
        return Result.ok(result);
    }
}